{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04516c8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import asyncio\n",
    "from mcp import ClientSession, StdioServerParameters\n",
    "from mcp.client.stdio import stdio_client\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from agents import Agent, Runner, trace, Tool\n",
    "from agents.mcp import MCPServerStdio\n",
    "from IPython.display import Markdown, display\n",
    "from datetime import datetime\n",
    "\n",
    "load_dotenv(override=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20b5482e",
   "metadata": {},
   "outputs": [],
   "source": [
    "server_params = {\"command\": \"python\", \"args\": [\"mcp-servers/assertion-server/assertion_server_python.py\"]}\n",
    "\n",
    "async with MCPServerStdio(params=server_params, client_session_timeout_seconds=60) as assertionServer:\n",
    "    assertion_tools = await assertionServer.list_tools()\n",
    "    result1 = await assertionServer.call_tool(\"assert_equals\", {\n",
    "            \"actual\": \"hello\",\n",
    "            \"expected\": \"helloo\"\n",
    "        })\n",
    "   \n",
    "print(assertion_tools)\n",
    "data = json.loads(result1.content[0].text)\n",
    "print(data)\n",
    "passes_value = data[\"passed\"]\n",
    "passes_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b358b11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ok, now try different way with session\n",
    "async def test_assertion_server():\n",
    "    # Connect to the assertion server\n",
    "    server_params = StdioServerParameters(\n",
    "        command=\"python\",\n",
    "        args=[\"mcp-servers/assertion-server/assertion_server_python.py\"]\n",
    "    )\n",
    "    \n",
    "    # Use async context manager (async with, not await)\n",
    "    async with stdio_client(server_params) as (read, write):\n",
    "        async with ClientSession(read, write) as session:\n",
    "            # Initialize the session\n",
    "            await session.initialize()\n",
    "            \n",
    "            # List available tools\n",
    "            print(\"📋 Available Tools:\")\n",
    "            tools_response = await session.list_tools()\n",
    "            for tool in tools_response.tools:\n",
    "                print(f\"  - {tool.name}: {tool.description}\")\n",
    "            \n",
    "            print(\"\\n🧪 Testing Assertions:\")\n",
    "            \n",
    "            # Test 1: assert_equals (should pass)\n",
    "            result1 = await session.call_tool(\"assert_equals\", {\n",
    "                \"actual\": \"hello\",\n",
    "                \"expected\": \"hello\"\n",
    "            })\n",
    "            print(f\"\\n1. assert_equals: {result1.content[0].text}\")\n",
    "            \n",
    "            # Test 2: assert_equals (should fail)\n",
    "            result2 = await session.call_tool(\"assert_equals\", {\n",
    "                \"actual\": \"hello\",\n",
    "                \"expected\": \"world\"\n",
    "            })\n",
    "            print(f\"\\n2. assert_equals (fail): {result2.content[0].text}\")\n",
    "            \n",
    "            # Test 3: assert_contains (should pass)\n",
    "            result3 = await session.call_tool(\"assert_contains\", {\n",
    "                \"text\": \"Hello World\",\n",
    "                \"substring\": \"World\"\n",
    "            })\n",
    "            print(f\"\\n3. assert_contains: {result3.content[0].text}\")\n",
    "            \n",
    "            # Test 4: assert_count\n",
    "            result4 = await session.call_tool(\"assert_count\", {\n",
    "                \"actual\": 5,\n",
    "                \"expected\": 5\n",
    "            })\n",
    "            print(f\"\\n4. assert_count: {result4.content[0].text}\")\n",
    "            \n",
    "            # Test 5: assert_greater_than\n",
    "            result5 = await session.call_tool(\"assert_greater_than\", {\n",
    "                \"actual\": 10,\n",
    "                \"expected\": 5\n",
    "            })\n",
    "            print(f\"\\n5. assert_greater_than: {result5.content[0].text}\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f828bacd",
   "metadata": {},
   "outputs": [],
   "source": [
    "await test_assertion_server()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf8328e6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
